package com.blockmeta.bbs.businesslibrary.websocket;



import com.blockmeta.bbs.businesslibrary.base.eventbus.EventBusBase;

import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantReadWriteLock;


/**
 * 不是对应交易所，而是对应与不同连线，由于当前只有一个连线，所以只有一个市场
 */
public class Market extends EventBusBase implements IConnect
{
	public interface IMarket
	{
		public void changeMarket(Market pMarket);
	}

	public enum AppMarket
	{
		COIN("¥",true); 		// 币市场
//		FC("¥", AppMarketKind.Futures, false, "market_name_fc", "popup_reset_account_rmb", "399300.SZ.FC", "999987.SH.FC", "399905.SZ.FC"), 	//

		String m_dollar;
		boolean m_humanReadableFormatEnabled;

		AppMarket(String dollar, boolean humanReadableFormatEnabled)
		{
			m_dollar = dollar;
			m_humanReadableFormatEnabled = humanReadableFormatEnabled;
		}

		public String getDollar()
		{
			return m_dollar;
		}

		public static AppMarket getAppMarket(String code)
		{
			if("coin".equals(code)) {
				return COIN;
			}
			else
				return COIN;
		}

		public boolean isHumanReadableFormatEnabled()
		{
			return m_humanReadableFormatEnabled;
		}

	}

	protected String	m_strCode;
	protected AppMarket m_enMarket;
	private ReentrantReadWriteLock m_lock;
	private ConcurrentHashMap<String, Symbol> m_mapSymbol;	// <SymbolCode, Symbol>


	Market(String strCode)
	{
		super();
		m_lock = new ReentrantReadWriteLock();
		m_strCode = strCode;
		m_enMarket = AppMarket.getAppMarket(strCode);
		m_mapSymbol = new ConcurrentHashMap<>();
	}

	public String getCode()
	{
		return m_strCode;
	}

	public Symbol getSymbol(String symbCode)
	{
		Symbol symbol = null;

//			m_lock.readLock().lock();


		try
		{

			symbol = m_mapSymbol.get(symbCode);
			if (symbol == null)
			{
//				m_lock.readLock().unlock();
//				m_lock.writeLock().lock();
				try
				{
					if (symbol == null)
					{
						symbol = new Symbol(this, symbCode);
						m_mapSymbol.put(symbCode, symbol);
					}

//					m_lock.readLock().lock();
				}
				finally
				{
//					m_lock.writeLock().unlock();
				}

			}
		}
		finally
		{
//			FDTLogUtils.e("chen",Thread.currentThread().getName());
//			Thread.dumpStack();
//				m_lock.readLock().unlock();
		}
		return symbol;
	}


	/**
	 * GetAllSymbols
	 * @return
	 */
	public ArrayList<Symbol> getSymbols()
	{
		try
		{
			m_lock.readLock().lock();
			return new ArrayList<Symbol>(m_mapSymbol.values());
		}
		finally
		{
			m_lock.readLock().unlock();
		}
	}

	@Override
	public void onConnectOK()
	{
		try
		{
			m_lock.readLock().lock();
			for (Symbol symbol : m_mapSymbol.values())
			{
				symbol.onConnectOK();
			}
		}
		finally
		{
			m_lock.readLock().unlock();
		}
	}

	@Override
	public void onConnectClose()
	{
		try
		{
			m_lock.readLock().lock();
			for (Symbol symbol : m_mapSymbol.values())
			{
				symbol.onConnectClose();
			}
		}
		finally
		{
			m_lock.readLock().unlock();
		}
	}

	public AppMarket getAppMarket()
	{
		return m_enMarket;
	}

	public boolean isEqual(Market market)
	{
		if (market!= null)
		{
			return m_strCode.equals(market.getCode());
		}
		return false;
	}
}
